package de.suur.jarmi.server;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import de.suur.jarmi.common.Response;

public class JarmiServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private RequestHandler requestHandler;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		Response response = requestHandler.handle(req.getInputStream());
		byte[] responseData = requestHandler.serialize(response);
		responseData = requestHandler.compress(responseData);
		requestHandler.writeResponse(resp.getOutputStream(), responseData);
	}

	public RequestHandler getRequestHandler() {
		return requestHandler;
	}

	public void setRequestHandler(RequestHandler requestHandler) {
		this.requestHandler = requestHandler;
	}

}
